package com.edu.dao.impl;

import com.edu.bean.User;
import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author:qiufeng
 * @Date: 2024/9/14 14:30
 * @Description:
 */


public class UserDaoImpl implements UserDao {


    @Override
    /**
     * 通过userName找存不存在
     */
    public int loginByUserName(String userName) {
        Object[] param = {userName};
        Connection conn=null;

        PreparedStatement s = null;
        ResultSet rs = null;
        try {
            conn=BaseDao.getConn();
            if(conn!=null) {
                System.out.println("连接成功");
            }
            String sql = "select * from user where userName='"+userName+"'";
            System.out.println(sql);
            s = conn.prepareStatement(sql);
            rs =  s.executeQuery();
            while(rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("userName"));
               if (user.getUsername()!=null){
                   System.out.println("存在账号");
                   return  1;
               }
                System.out.println("不存在账号");
                return -1;
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, s,rs);
        }
        return -1;

    }

    @Override
    public User loginByUserNameAndUserPass(String userName, String userPass) {

        PreparedStatement s = null;
        ResultSet rs = null;
        User user=null;
        Connection conn=null;
        try {
            conn=BaseDao.getConn();
            if(conn!=null) {
                System.out.println("连接成功");
            }
            String sql = "select * from user where username='"+userName+"' and userpass='"+userPass+"'";
            System.out.println(sql);
            s = conn.prepareStatement(sql);
            System.out.println("111111");
            rs =  s.executeQuery();
            while(rs.next()) {
                user = new User();

                user.setUserId(rs.getInt("userId"));
                user.setUsername(rs.getString("userName"));
                user.setUserpass(rs.getString("userPass"));
                user.setRealName(rs.getString("realName"));

            }

            if (user==null){
                System.out.println("密码错误");
                user=new User();
                user.setUserId(-1);
                return user;
            }
            return user;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(conn, s,rs);
        }
        return null;
    }

    @Override
    public int rigister(User user) {
        Object[] param = {user.getUsername(),user.getUserpass(),user.getRealName()};
        String sql = "INSERT INTO user(userName,userPass,realName) VALUES(?,?,?);";
        int num = BaseDao.executeUpdate(sql, param);
        return num;

    }


}
